/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.pixlib.sfe.display 
{
	import net.pixlib.core.PixlibApplication;
	import net.pixlib.model.ModelEvent;
	import net.pixlib.model.ModelListener;
	import net.pixlib.sfe.event.UnicodeTableEvent;
	import net.pixlib.sfe.log.Debug;
	import net.pixlib.sfe.util.getTranslation;
	import net.pixlib.tms.LocalManager;
	import net.pixlib.tms.LocalManagerEvent;

	import org.aswing.BorderLayout;
	import org.aswing.Insets;
	import org.aswing.JButton;
	import org.aswing.JFrame;
	import org.aswing.JList;
	import org.aswing.JPanel;
	import org.aswing.JScrollPane;
	import org.aswing.border.EmptyBorder;

	import flash.events.Event;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.DropShadowFilter;

	/**
	 * 'UnicodeSelector' dialog view.
	 * 
	 * @author Romain Ecarnot
	 */
	public class UnicodeSelector extends JFrame implements ModelListener
	{
		//--------------------------------------------------------------------
		// Event types
		//--------------------------------------------------------------------
		
		/**
		 * 
		 */
		public static const onTranslatedEVENT : String = "onTranslated";
		
				
		//--------------------------------------------------------------------
		// Private properties
		//--------------------------------------------------------------------
		
		/** @private */
		private static var _instance : UnicodeSelector;

		/** @private */
		private var _list : JList;

		/** @private */
		private var _clearSelection : JButton; 
		
		[ArrayElementType("String")]
		/** @private */
		private var _labelList : Array; 

		
		//--------------------------------------------------------------------
		// Public API
		//--------------------------------------------------------------------
		
		/**
		 * Returns UnicodeSelector singleton instance.
		 */
		public static function getInstance() : UnicodeSelector
		{
			if(!_instance) _instance = new UnicodeSelector();
			return _instance;
		}

		/**
		 * Opens selector view with passed-in selected indices.
		 * 
		 * @param rangeList	indices to select
		 */
		public function open(rangeList : Array = null) : void
		{
			if(rangeList != null)
			{
				_list.setSelectedIndices(rangeList);
			}
			else _list.setSelectedIndex(-1);
			
			setX(PixlibApplication.getInstance().size.width * 0.5 - width * 0.5);
			setY(PixlibApplication.getInstance().size.height * 0.5 - height * 0.5);
			
			super.show();
		}

		/**
		 * Returns selected indices.
		 */
		public function getSelection() : Array
		{
			return _list.getSelectedIndices().concat([]);
		}

		/**
		 * Triggered when Unicode Table has changed.
		 */
		public function onUpdateUnicodeTable(event : UnicodeTableEvent) : void
		{
			var selections : Array = _list.getSelectedIndices();
			
			_labelList = event.getTable();
			
			_list.setListData(_labelList);
			_list.setSelectedIndices(selections);
			
			dispatchEvent(new Event(onTranslatedEVENT));
		}

		
		//--------------------------------------------------------------------
		// Protected methods
		//--------------------------------------------------------------------

		/**
		 * Inits User Interface.
		 */
		protected function init( ) : void
		{
			LocalManager.getInstance().addEventListener(LocalManagerEvent.onLocalChangeEVENT, _onLocalChangeHandler);
			
			setContentPane(new JPanel(new BorderLayout(5, 5)));
			setResizable(false);
			setClosable(true);
			filters = [new DropShadowFilter(0, 0, 0x000000, 0.75, 10, 10, 1, BitmapFilterQuality.MEDIUM)];
			setSizeWH(275, 200);
			
			_list = new JList([]);
			_list.setSelectionMode(JList.MULTIPLE_SELECTION);
			
			_clearSelection = new JButton("clear");
			_clearSelection.addActionListener(_onClearSelectionHandler);
			
			getContentPane().setBorder(new EmptyBorder(null, new Insets(5, 5, 5, 5)));
			getContentPane().append(new JScrollPane(_list), BorderLayout.CENTER);			getContentPane().append(_clearSelection, BorderLayout.SOUTH);
			
			_onLocalChangeHandler();
		}
		
		/**
		 *
		 */
		public function getLabelAt(index : uint) : String
		{
			Debug.DEBUG("Retreive unicode label at " + index, this);
			
			return _labelList[index];	
		}
		
		/** @private */
		public function onReleaseModel(event : ModelEvent) : void
		{
		}
		
		/** @private */
		public function onInitModel(event : ModelEvent) : void
		{
		}

		
		//--------------------------------------------------------------------
		// Private methods
		//--------------------------------------------------------------------
		
		/** @private */
		function UnicodeSelector()
		{
			super(PixlibApplication.getInstance().root, "Unicode selector", true);
			
			init();
		}

		/** @private */
		private function _onLocalChangeHandler(event : LocalManagerEvent = null) : void
		{
			setTitle(getTranslation("unicodeDialogTitle"));
			
			_clearSelection.setText(getTranslation("unicodeClearButton"));
		}

		/** @private */
		private function _onClearSelectionHandler(event : Event) : void
		{
			_list.setSelectedIndex(-1);
		}
	}
}
